package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.dto.NoticeListDTO;
import cn.ulc.socket.admin.model.param.NoticeParam;
import cn.ulc.socket.admin.service.notice.NoticeServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@RequestMapping("/notice")
public class NoticeCtrl {

  private static final Log log = LogFactory.get();

  @Autowired
  NoticeServ noticeServ;

  @GetMapping
  public Result findListPage(String startDate,String endDate, PaginationParam pager) {
    NoticeListDTO dto = noticeServ.findListPage(startDate,endDate,pager);
    return Result.success(dto);
  }

  @PostMapping
  public Result save(@Validated @RequestBody NoticeParam param, BindingResult result){
    if(result.hasErrors()) {
      log.error("保存公告详情参数错误",result.getFieldError().getDefaultMessage());
      return Result.fail(result.getFieldError().getDefaultMessage());
    }
    return Result.success(noticeServ.save(param));
  }
  @PutMapping("/{id}")
  public Result update(@PathVariable("id") Long id, @RequestBody NoticeParam param, BindingResult result){
    if(result.hasErrors()) {
      log.error("保存公告详情参数错误",result.getFieldError().getDefaultMessage());
      return Result.fail(result.getFieldError().getDefaultMessage());
    }
    param.setId(id);
    return Result.success(noticeServ.save(param));
  }
  @DeleteMapping("/{id}")
  public Result deleted(@PathVariable("id") Long id){
    return Result.success(noticeServ.deleted(id));
  }

  @PutMapping("/changeStatus/{id}")
  public Result changeStatus(@PathVariable("id") Long id, AvailableStatus status){
    return Result.success(noticeServ.changeStatus(id,status));
  }
}
